package com.yanqu.road.dataHelper.action.excel;

import com.yanqu.road.dataHelper.gameServer.GameServer;
import com.yanqu.road.dataHelper.manager.BaseMgr;
import com.yanqu.road.dataHelper.manager.ConfigMgr;
import com.yanqu.road.dataHelper.manager.DataMgr;
import com.yanqu.road.dataHelper.repair.BaseRepair;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author xx
 * @Date 2020/11/11
 */
public class SecretVolumeNoPei extends BaseRepair {

    public SecretVolumeNoPei(GameServer gameServer) {
        super(gameServer);
    }

    @Override
    public void repair(long param1, String param2) {

    }

    public void go(){
        String no = "22";
        List<Map<String,String>> needBuSecretVolumeMap = new ArrayList<>();
        List<Map<String,String>> secretVolumeMapList = getSecretVolume(no);
        System.err.println("SecretVolumeNoPei:"+secretVolumeMapList.size());
        for(Map<String,String> data : secretVolumeMapList){
            String userId = data.get("userId");
            Map<String,Map<String,String>> secretVolumeMap = BaseMgr.getMgr(ConfigMgr.class).getCanGetRewardSecretVolumeMap(Integer.valueOf(data.get("exp")));
            for(Map<String,String> infoMap : secretVolumeMap.values()){
                if(getButNoGetReward(userId,infoMap.get("id"),data.get("startDate"),data.get("endDate"))){
                    infoMap.put("userId",userId);
                    needBuSecretVolumeMap.add(infoMap);
                }
            }
        }

        DataMgr.addData(needBuSecretVolumeMap);
    }

    private List<Map<String,String>> getSecretVolume(String no) {
        List<Map<String,String>> tempData = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select userId,`no`,exp,startDate,DATE_ADD(startDate, INTERVAL dateLength DAY) as endDate from t_u_secret_volume where `no` = ? AND isBuy = 1";
        try {
            call = getGameServer().getConnection().prepareCall(sql);
            int i=1;
            call.setString(i++,no);
            rs = call.executeQuery();
            while (rs.next()) {
                Map<String,String> dd = new ConcurrentHashMap<>();
                dd.put("userId",rs.getString("userId"));
                dd.put("exp",rs.getString("exp"));
                dd.put("startDate",rs.getString("startDate"));
                dd.put("endDate",rs.getString("endDate"));
                tempData.add(dd);
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return tempData;
    }

    private boolean getButNoGetReward(String userId,String taskId,String startDate,String endDate) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_l_task_reward where userId = ? and taskId = ? and `time` > ? AND `time` < ? AND reward = ''";
        try {
            call = getGameServer().getLogConnection().prepareCall(sql);
            int i=1;
            call.setString(i++,userId);
            call.setString(i++,taskId);
            call.setString(i++,startDate);
            call.setString(i++,endDate);
            rs = call.executeQuery();
            while (rs.next()) {
                return true;
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误1", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return false;
    }
}
